package com.taxshare.seller.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.response.beans.Response;
import com.taxshare.seller.entity.SellerInvoiceDetailEntity;
import com.taxshare.seller.entity.SellerInvoiceEntity;
import com.taxshare.seller.mapper.InvoicedMapper;
import com.taxshare.seller.view.dto.ToBeOpenInvoiceDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author YANG
 * @date 2021/3/25
 * @Description:待开发票管理
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
@Slf4j
public class ToBeOpenInvoiceService extends ServiceImpl<InvoicedMapper, SellerInvoiceEntity> {

    @Autowired
    InvoiceDetailService invoiceDetailService;

    /**
     * 待开发票查询
     *
     * @param query
     * @return
     */
    public Response toBeOpenInvoiceQuery(PageRequest<ToBeOpenInvoiceDTO> query) {
        QueryWrapper<SellerInvoiceEntity> queryWrapper = new QueryWrapper<>();
        ToBeOpenInvoiceDTO toBeOpenInvoiceDTO = query.getInfo();
        if (StrUtil.isNotBlank(toBeOpenInvoiceDTO.getInvoiceType())) {
            queryWrapper.eq("invoice_type", toBeOpenInvoiceDTO.getInvoiceType());
        }
        if (StrUtil.isNotBlank(toBeOpenInvoiceDTO.getMakeInvoiceType())) {
            queryWrapper.eq("make_invoice_type", toBeOpenInvoiceDTO.getMakeInvoiceType());
        }
        if (StrUtil.isNotBlank(toBeOpenInvoiceDTO.getInvoiceState())) {
            queryWrapper.eq("invoice_state", toBeOpenInvoiceDTO.getInvoiceState());
        }
        if (StrUtil.isNotBlank(toBeOpenInvoiceDTO.getSellerName())) {
            queryWrapper.eq("seller_name", toBeOpenInvoiceDTO.getSellerName());
        }
        if (StrUtil.isNotBlank(toBeOpenInvoiceDTO.getSellerTaxNo())) {
            queryWrapper.eq("seller_tax_no", toBeOpenInvoiceDTO.getSellerTaxNo());
        }
        if (StrUtil.isNotBlank(toBeOpenInvoiceDTO.getPurchaserName())) {
            queryWrapper.like("purchaser_name", toBeOpenInvoiceDTO.getPurchaserName());
        }
        if (StrUtil.isNotBlank(toBeOpenInvoiceDTO.getPurchaserTaxNo())) {
            queryWrapper.eq("purchaser_tax_no", toBeOpenInvoiceDTO.getPurchaserTaxNo());
        }
        if (StrUtil.isNotBlank(toBeOpenInvoiceDTO.getDataSource())) {
            queryWrapper.eq("data_source", toBeOpenInvoiceDTO.getDataSource());
        }
        if (StrUtil.isNotBlank(toBeOpenInvoiceDTO.getCreateTimeStart()) && StrUtil.isNotBlank(toBeOpenInvoiceDTO.getCreateTimeEnd())) {
            queryWrapper.between("create_time_str", toBeOpenInvoiceDTO.getCreateTimeStart(), toBeOpenInvoiceDTO.getCreateTimeEnd());
        }
        queryWrapper.eq("make_invoice_state", "0");
        queryWrapper.orderByDesc("create_time");
        IPage<SellerInvoiceEntity> sellerInvoiceEntityIPage = PageHelp.siamplePage(this, queryWrapper, query.getPage(), SellerInvoiceEntity.class);
        return Response.success(sellerInvoiceEntityIPage);
    }

    /**
     * 待开发票明细查询
     *
     * @param query
     * @return
     */
    public Response queryDetail(PageRequest<ToBeOpenInvoiceDTO> query) {
        QueryWrapper<SellerInvoiceDetailEntity> queryWrapper = new QueryWrapper<>();
        IPage<SellerInvoiceDetailEntity> page = new Page<>(query.getPage().getCurrentPage(), query.getPage().getPageSize());
        queryWrapper.eq("serial_no", query.getInfo().getSerialNo());
        queryWrapper.orderByAsc("detail_no");
        IPage<SellerInvoiceDetailEntity> sellerInvoiceDetailEntityIPage = invoiceDetailService.page(page, queryWrapper);
        return Response.success(sellerInvoiceDetailEntityIPage);
    }

    /**
     * 修改发票备注
     *
     * @param toBeOpenInvoiceDTO
     * @return
     */
    public Response editRemark(ToBeOpenInvoiceDTO toBeOpenInvoiceDTO) {
        QueryWrapper<SellerInvoiceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("serial_no", toBeOpenInvoiceDTO.getSerialNo());
        SellerInvoiceEntity sellerInvoiceEntity = baseMapper.selectOne(queryWrapper);
        if (sellerInvoiceEntity != null) {
            sellerInvoiceEntity.setRemark(toBeOpenInvoiceDTO.getRemark());
        }
        baseMapper.update(sellerInvoiceEntity, queryWrapper);
        return Response.success();
    }

}
